import request from '@/utils/request'
import axios from 'axios'

export function listiorders(data) {
  return request({
    url: '/dailyOrder/list',
    method: 'post',
    data
  })
}
export function listiorders2(data) {
  return request({
    url: '/iorder/list',
    method: 'post',
    data
  })
}
export function getEditInfos1(data) {//根据订量变化计算分装体积变化
  return request({
    url: '/iorder/getEditInfo1',
    method: 'post',
    data
  })
}
export function getEditInfos2(data) {//根据分装体积变化计算订量变化
  return request({
    url: '/iorder/getEditInfos1',
    method: 'post',
    data
  })
}
export function getEditInfos3(data) {//根据分装体积变化计算订量变化
  return request({
    url: '/iorder/getEditInfo3',
    method: 'post',
    data
  })
}

export function getEditInfos4(data) {//根据分装体积变化计算订量变化
  return request({
    url: '/iorder/getEditInfo4',
    method: 'post',
    data
  })
}
export function getEditInfos5(data) {//根据分装体积变化计算订量变化
  return request({
    url: '/iorder/getEditInfo5',
    method: 'post',
    data
  })
}


export function deleteiorders(data) {
  return request({
    url: '/iorder/delete',
    method: 'post',
    data
  })
}

export function showDateOrders(query) {
  return request({
    url: '/iorder/showDateOrders',
    method: 'get',
    params: query
  })
}

export function uploadExcels(data) {
  return request({
    url: '/iorder/uploadExcels',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data
  })
}
export function downloadExcels(data) {
  return request({
    url: '/iorder/downloadExcel',
    method: 'get',
    data
  })
}

export const uploadFileRequest = (url, params) => {
  return axios({
    method: 'post',
    url: '/iorder/insertMany',
    data: params,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function publishiorder(data) {
  return request({
    url: '/iorders/create',
    method: 'post',
    data
  })
}

export function insertAlliorder(data) {
  return request({
    url: '/iorder/insertMany',
    method: 'post',
    data
  })
}

export function detailiorders(id) {
  return request({
    url: '/iorder/detail',
    method: 'get',
    params: { id }
  })
}

export function editiorders(data) {
  return request({
    url: '/iorder/update',
    method: 'post',
    data
  })
}
export function moreiorders(data) {
  return request({
    url: '/iorder/more',
    method: 'post',
    data
  })
}

export function getLocalIPv4() { // 获取本机IPv4
  if (typeof window !== 'undefined') {
    var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection

    if (RTCPeerConnection) {
      (() => {
        var rtc = new RTCPeerConnection()
        rtc.createDataChannel('') // 创建一个可以发送任意数据的数据通道
        rtc.createOffer(offerDesc => { // 创建并存储一个sdp数据
          rtc.setLocalDescription(offerDesc)
        }, e => { console.log(e) })

        rtc.onicecandidate = (evt) => { // 监听candidate事件
          if (evt.candidate) {
          // console.log('evt:',evt.candidate)
            const ip_rule = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
            const ip_addr = ip_rule.exec(evt.candidate.candidate)
            if (ip_addr) {
              sessionStorage.setItem('ip', ip_addr[1]) // 保存 本机IP
            }
            // console.log('ip_addr:',ip_addr)   //打印获取的IP地址4
            return 'http://' + ip_addr[1] + '/demo/admin/iorder/uploadExcels'
          }
        }
      })()
    } else { console.log('没有找到') }
  }
}

export function listCatAndBrand() {
  return request({
    url: '/iorder/catAndBrand',
    method: 'get'
  })
}

export function myformatdate(inputTime) {
  if (!inputTime && typeof inputTime !== 'number') {
    return ''
  }
  var localTime = ''
  inputTime = new Date(inputTime).getTime()
  const offset = (new Date()).getTimezoneOffset()
  localTime = (new Date(inputTime - offset * 60000)).toISOString()
  localTime = localTime.substr(0, localTime.lastIndexOf('.'))
  localTime = localTime.replace('T', ' ')
  return localTime
}
export function formatterTime(row, column) {
  const data = row[column.property]
  return /\d{4}-\d{1,2}-\d{1,2}/g.exec(data)
}

export function uploadFile(data) {
  return request({
    method: 'post',
    url: '/iorder/insertMany',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}
